package cn.darkhorse.mesadmin.controller.ticket;


import cn.darkhorse.ticket.basis.pojo.TicketProduct;
import cn.darkhorse.ticket.basis.service.TicketProductService;
import cn.darkhorse.ticket.code.RepeatCheck;
import cn.darkhorse.ticket.constants.TicketConstants;
import cn.darkhorse.ticket.basis.vo.TicketProductVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 产品信息 前端控制器
 * </p>
 *
 * @author wangxinghui
 * @since 2024-01-09
 */
@RestController
@RequestMapping("/mesticket/basisProduct")
public class TicketProductController extends BaseController{

	@Resource
	private TicketProductService productService;
	@Resource
	private RepeatCheck<TicketProduct> repeatCheck;
	/**
	 * 列表
	 * @param code
	 * @param name
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public TableDataInfo list(String code, String name, String model,Long typeId){
		startPage();
		LambdaQueryWrapper<TicketProduct> queryWrapper = new LambdaQueryWrapper<>();
		if(StringUtils.isNotBlank(code)){
			queryWrapper.like(TicketProduct::getCode,code);
		}
		if(StringUtils.isNotBlank(name)){
			queryWrapper.like(TicketProduct::getName,name);
		}
		if(StringUtils.isNotBlank(model)){
			queryWrapper.like(TicketProduct::getModel,model);
		}
		if(null != typeId){
			queryWrapper.eq(TicketProduct::getProductTypeId,typeId);
		}
		queryWrapper.orderByDesc(TicketProduct::getGmtCreate);
		List<TicketProduct> productList = productService.list(queryWrapper);

		return getDataTable(productList);
	}

	/**
	 * 新增
	 * @return
	 */
	@RepeatSubmit()
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public AjaxResult add(@RequestBody TicketProduct ticketProduct){
		//String s = productService.repeat(ticketProduct);
		String s = repeatCheck.repeat(productService, ticketProduct, TicketConstants.BUSINESS_TYPE_CP);
		if(StringUtils.isNotBlank(s)){
			ticketProduct.setCode(s);
		}
		productService.save(ticketProduct);
		return AjaxResult.success();
	}

	/**
	 * 修改
	 * @param ticketProduct
	 * @return
	 */
	@RepeatSubmit()
	@RequestMapping(value = "/modify",method = RequestMethod.POST)
	public AjaxResult modify(@RequestBody TicketProduct ticketProduct){
		boolean quote = productService.isQuote(ticketProduct.getId());
		if(!quote){
			return AjaxResult.error("该产品已关联了工单或计划，无法操作！");
		}
		//校验名称
		productService.repeat(ticketProduct);
		productService.updateById(ticketProduct);
		return AjaxResult.success();
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RepeatSubmit()
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public AjaxResult delete(Long id){
		if(Objects.isNull(id)){
			return AjaxResult.error("缺失参数值！");
		}
		boolean quote = productService.isQuote(id);
		if(!quote){
			return AjaxResult.error("该产品已关联了工单或计划，无法操作！");
		}
		productService.removeById(id);
		return AjaxResult.success();
	}


	/**
	 *查看
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/view",method = RequestMethod.GET)
	public AjaxResult view(Long id){
		if(Objects.isNull(id)){
			return AjaxResult.error("缺失参数值！");
		}
		TicketProductVO productVO = productService.view(id);
		return AjaxResult.success(productVO);
	}


	/**
	 * 是否引用
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/is-quote",method = RequestMethod.GET)
	public AjaxResult isQuote(Long id){
		if(Objects.isNull(id)){
			return AjaxResult.error("缺失参数值！");
		}
		boolean quote = productService.isQuote(id);
		return AjaxResult.success(quote);
	}
	/**
	 * 是否关联工序
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/has-produce",method = RequestMethod.GET)
	public AjaxResult hasProduce(Long id){
		if(Objects.isNull(id)){
			return AjaxResult.error("缺失参数值！");
		}
		boolean quote = productService.hasProduce(id);
		return AjaxResult.success(quote);
	}

}

